home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_20 / mf1to0.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  14KB  |  526 lines

  1. /*
  2.  * f1to0.c 4/30/89
  3.  *
  4.  * Author:  Michael S. Czeiszperger
  5.  * Purpose: The program converts format 1 MIDI files to format 0
  6.  *          MIDI files.  It is meant to demonstrate the use of
  7.  *          the midifile library.  The internal data format is
  8.  *          a linked list of MIDI and SMF meta events.
  9.  *
  10.  *          The spec is available from:
  11.  *               International MIDI Association
  12.  *               5316 West 57th Street
  13.  *               Los Angeles, CA 90056
  14.  *
  15.  *          An in-depth description of the spec can also be found
  16.  *          in the article "Introducing Standard MIDI Files", published
  17.  *          in Electronic Musician magazine, April, 1988.
  18.  * 
  19.  * usage: f1to0 infile outfile
  20.  * 
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #ifdef THINK
  27. #include <stdlib.h>
  28. #else
  29. #include <malloc.h>
  30. #endif
  31.  
  32. #include "midifile.h"
  33.  
  34. enum PACKET_TYPE { MIDI_EVENT, META_EVENT };
  35.  
  36. struct MIDIpacket        /* Stucture for MIDI events */
  37. {                             
  38.     enum PACKET_TYPE p_type; /* What type of packet is it?               */
  39.     unsigned long time;      /* absolute time of event, in ms increments */
  40.     unsigned int m_type;     /* What kind of MIDI message?               */
  41.     unsigned int chan;       /* The MIDI channel                         */
  42.     unsigned long len;       /* length of the midi message               */
  43.     unsigned char *data;     /* pointer to the data                      */
  44.     struct MIDIpacket *next,*before;           /* point to next packet */
  45. } *sequence_start,*sequence_end;
  46.  
  47. /* 
  48.    This example structure is not very portable. It does, however,
  49.    mirror the logical operation of the header, and on some machines can
  50.    be written and read directly.  It assumes that shorts are at least
  51.    two bytes and chars are at least one byte.
  52. */
  53. struct header_type {
  54.     short format;
  55.     short ntrks;
  56.     union {
  57.         short quarter_note;
  58.         struct{
  59.             char format;
  60.             char resolution;
  61.             } smpte;
  62.         } division;
  63. } header;
  64.  
  65. /* These lines are needed to use the library */
  66. FILE *fpIn,*fpOut;
  67. extern long Mf_currtime;
  68. mygetc() { return(getc(fpIn)); }
  69. myputc(c) { return(putc(c,fpOut));}
  70.  
  71. main(argc,argv)
  72. int argc;
  73. char **argv;
  74. {
  75. /* This is included of an example of what to do if you use THINK C on
  76.    the mac. */
  77. #ifdef THINK
  78.     #include <console.h>
  79.         argc = ccommand(&argv);
  80. #endif
  81.  
  82.     if((fpIn = fopen(argv[1],"r")) == 0L)
  83.     printf("f1to0: unable to open file %s for reading.\n",argv[1]);
  84.  
  85.     if((fpOut = fopen(argv[2],"w")) == 0L)
  86.     printf("f1to0: unable to open file %s for writing.\n",argv[2]);
  87.  
  88.     init_funcs();
  89.  
  90.     /* read the midi file */
  91.     mfread();
  92.  
  93.     /* write a single track */
  94.     mfwrite((int)header.format,1,(int)header.division.quarter_note,fpOut);
  95. }
  96.  
  97. myheader(format,ntrks,division)
  98. int format,ntrks,division;
  99. {
  100.     header.format = format;
  101.     header.ntrks = ntrks;
  102.     header.division.quarter_note = division;
  103. }
  104.  
  105. myerror(s)
  106. char *s;
  107. {
  108.     printf("%10ld: %s\n",Mf_currtime,s);
  109. }
  110.  
  111. /*
  112.  * mywritetrack()
  113.  *
  114.  * Sample showing how to use the library routines to write out a track.
  115.  * Returns 1 if successful, and -1 if not.
  116.  *
  117.  */
  118. int mywritetrack(track)
  119. int track;
  120. {
  121.     struct MIDIpacket *current;
  122.     unsigned long delta_time;
  123.  
  124.     current = sequence_start; /* init to point at the beginning of the list */
  125.  
  126.     /* shuffle through each element in the linked list, writing out
  127.        the data in the standard MIDI format */
  128.     while(current != 0L)
  129.     {
  130.         if(current == sequence_start)
  131.            delta_time = current->time;
  132.         else
  133.            delta_time = current->time - (current->before)->time;
  134. #ifdef DEBUG
  135. printf("ticks = %lu event = %d packet = %d chan = %d len = %lu\n",
  136. current->time,current->m_type,current->p_type,current->chan,current->len);
  137. #endif
  138.  
  139.     if(current->p_type == MIDI_EVENT)
  140.     {
  141.         if(mf_write_midi_event(delta_time,current->m_type,current->chan,current->data,current->len) < 0)
  142.             return(-1);
  143.     }
  144.     else if(current->p_type == META_EVENT)
  145.     {
  146.         if(mf_write_meta_event(delta_time,current->m_type,current->data,current->len) < 0)
  147.             return(-1);
  148.     }
  149.     else
  150.         fprintf(stderr,"Unknown MIDI packet encounted.\n");
  151.  
  152.         current = current->next;
  153.     }
  154.     return(1);
  155. } /* end of write_track() */
  156.  
  157.  
  158. /*
  159.  * This routine adds a MIDI or META event into the linked list.  
  160.  */
  161. void add_packet(ticks,event_type,packet_type,chan,data,len)
  162. unsigned long ticks;                 /* absolute ticks of event */
  163. int event_type;                      /* note_on, note_off, etc  */
  164. enum PACKET_TYPE packet_type;        /* What kind of data?      */
  165. unsigned char *data;                 /* already allocated data  */
  166. unsigned long len;                   /* length of data          */
  167. {
  168.     struct MIDIpacket *scratch,*insert_pt,*get_insert_point();
  169.  
  170. #ifdef DEBUG
  171. printf("ticks = %d event = %d packet = %d chan = %d len = %d\n",ticks,event_type,packet_type,chan,len);
  172. #endif
  173.     scratch = (struct MIDIpacket *) malloc(sizeof(struct MIDIpacket));
  174.  
  175.     if(scratch == NULL)
  176.     {
  177.           fprintf(stderr,"Sorry, out of memory!\n");
  178.           exit(1);
  179.     }
  180.  
  181.     scratch->data = data;
  182.     scratch->m_type = event_type;
  183.     scratch->p_type = packet_type;
  184.     scratch->chan = chan;
  185.     scratch->len  = len;
  186.     scratch->time = ticks;
  187.     
  188.     /* Where in the linked list should this packet go? */
  189.     insert_pt = get_insert_point(sequence_start,sequence_end,ticks);
  190.    
  191.     if(sequence_start == 0L)       /* the list is empty */
  192.     {  
  193.         sequence_start = sequence_end = scratch;
  194.         scratch->before = 0L;
  195.         scratch->next = 0L;
  196.     }
  197.     else if(insert_pt == 0L) /* insert the new bottom record */
  198.     { 
  199.         sequence_end->next = scratch;
  200.         scratch->before = sequence_end;
  201.         scratch->next = 0L;
  202.         sequence_end = scratch;
  203.     }
  204.     else if(insert_pt == sequence_start) /* insert the new top record */
  205.     {      
  206.         sequence_start->before = scratch;
  207.         scratch->before = 0L;
  208.         scratch->next = sequence_start;
  209.         sequence_start = scratch;
  210.     }
  211.     else  /* insert before the insert pointer */
  212.     {      
  213.         scratch->next = insert_pt;
  214.         scratch->before = insert_pt->before;
  215.         insert_pt->before = scratch;
  216.         (scratch->before)->next = scratch;
  217.     }
  218. } /* end of add_packet() */
  219.  
  220. /*
  221.  * get_insert_point()
  222.  *    A routine to help insertion into a linked list,  
  223.  *    by returning a pointer to the position in the list
  224.  *    where the MIDI packet should be inserted based on
  225.  *    the time the MIDI event occurs. This could be a lot
  226.  *    faster, but was left simple for easy debugging.
  227.  *    If you'd like to speed this up, please go ahead!
  228.  */
  229. struct MIDIpacket *get_insert_point(top,bottom,time)
  230. struct MIDIpacket *top,*bottom;
  231. unsigned long time;
  232. {
  233.     struct MIDIpacket *index;
  234.     /* this keeps track of the last inset point, making it easier */
  235.     /* to find the next one assuming that most times will be     */
  236.     /* consecutive.                                               */
  237.     static struct MIDIpacket *insert_pointer;
  238.     
  239.     if(top == 0L || bottom == 0L) return(0L);
  240.     if(time > bottom->time) return(0L); /* tack onto end of list */
  241.     if(time < top->time) return(top); /* insert as the new first record */    
  242.  
  243.     /* check around where the last packet was inserted */
  244.     if(insert_pointer != 0L)
  245.         index = insert_pointer;
  246.     else
  247.     {
  248.         index = sequence_start;
  249.         insert_pointer = index;
  250.     }
  251.     if(time > index->time)
  252.     {
  253.         while(time > index->time && index != 0L)
  254.         index = index->next;
  255.         return(index);
  256.     }
  257.     else if(time < index->time)
  258.     {
  259.         while(time < index->time && index != 0L)
  260.         index = index->before;
  261.         return(index->next);
  262.     }
  263.     else 
  264.     return(insert_pointer);
  265. } /* end of get_insert_point() */
  266.  
  267. /* Routines called from midifile lib */
  268. mynoteon(chan,c1,c2)
  269. int chan,c1,c2;
  270. {
  271.     unsigned char *data;
  272.  
  273.     data = (unsigned char *) malloc(sizeof(char) * 2);
  274.     if(data == NULL)
  275.     {
  276.           fprintf(stderr,"Sorry, out of memory!\n");
  277.           exit(1);
  278.     }
  279.     data[0] = (unsigned char) c1;
  280.     data[1] = (unsigned char) c2;
  281.     add_packet((unsigned long)Mf_currtime,note_on,MIDI_EVENT,chan,data,(unsigned long)2);
  282. }
  283.  
  284. mynoteoff(chan,c1,c2)
  285. int chan,c1,c2;
  286. {
  287.     unsigned char *data;
  288.  
  289.     data = (unsigned char *) malloc(sizeof(char) * 2);
  290.     if(data == NULL)
  291.     {
  292.           fprintf(stderr,"Sorry, out of memory!\n");
  293.           exit(1);
  294.     }
  295.     data[0] = (unsigned char) c1;
  296.     data[1] = (unsigned char) c2;
  297.     add_packet((unsigned long)Mf_currtime,note_off,MIDI_EVENT,chan,data,(unsigned long)2);
  298. }
  299.  
  300. mypressure(chan,pitch,pressure)
  301. int chan,pitch,pressure;
  302. {
  303.     unsigned char *data;
  304.  
  305.     data = (unsigned char *) malloc(sizeof(char) * 2);
  306.     if(data == NULL)
  307.     {
  308.           fprintf(stderr,"Sorry, out of memory!\n");
  309.           exit(1);
  310.     }
  311.     data[0] = (unsigned char) pitch;
  312.     data[1] = (unsigned char) pressure;
  313.     add_packet((unsigned long)Mf_currtime,poly_aftertouch,MIDI_EVENT,chan,data,(unsigned long)2);
  314. }
  315.  
  316. mykeypressure(chan,pitch,pressure)
  317. int chan,pitch,pressure;
  318. {
  319.     unsigned char *data;
  320.  
  321.     data = (unsigned char *) malloc(sizeof(char) * 2);
  322.     if(data == NULL)
  323.     {
  324.           fprintf(stderr,"Sorry, out of memory!\n");
  325.           exit(1);
  326.     }
  327.     data[0] = (unsigned char) pitch;
  328.     data[1] = (unsigned char) pressure;
  329.     add_packet((unsigned long)Mf_currtime,channel_aftertouch,MIDI_EVENT,chan,data,(unsigned long)2);
  330. }
  331.  
  332. myparameter(chan,control,value)
  333. int chan,control,value;
  334. {
  335.     unsigned char *data;
  336.  
  337.     data = (unsigned char *) malloc(sizeof(char) * 2);
  338.     if(data == NULL)
  339.     {
  340.           fprintf(stderr,"Sorry, out of memory!\n");
  341.           exit(1);
  342.     }
  343.     data[0] = (unsigned char) control;
  344.     data[1] = (unsigned char) value;
  345.     add_packet((unsigned long)Mf_currtime,control_change,MIDI_EVENT,chan,data,(unsigned long)2);
  346. }
  347.  
  348. mytempo(microsecs)
  349. unsigned long microsecs;
  350. {
  351.     unsigned char *data;
  352.     data = (unsigned char *) malloc(sizeof(char) * 3);
  353.     if(data == NULL)
  354.     {
  355.           fprintf(stderr,"Sorry, out of memory!\n");
  356.           exit(1);
  357.     }
  358.     data[2] = (unsigned)(microsecs & 0xff);
  359.     data[1] = (unsigned)((microsecs >> 8) & 0xff);
  360.     data[0] = (unsigned)((microsecs >> 16) & 0xff);
  361.     add_packet((unsigned long)Mf_currtime,set_tempo,META_EVENT,0,data,(unsigned long)3);
  362. }
  363.  
  364. myvarlen(type,len,msg)
  365. int type,len;
  366. char *msg;
  367. {
  368.     unsigned char *data;
  369.     int i;
  370.  
  371.     data = (unsigned char *) malloc((unsigned)(sizeof(char) * len));
  372.     if(data == NULL)
  373.     {
  374.           fprintf(stderr,"Sorry, out of memory!\n");
  375.           exit(1);
  376.     }
  377.     for(i = 0; i < len; i++)
  378.         data[i] = msg[i];
  379.     add_packet((unsigned long)Mf_currtime,type,META_EVENT,0,data,(unsigned long)len);
  380. }
  381.  
  382. mysmpte(hour,min,sec,frame,fract)
  383. char hour,min,sec,frame,fract;
  384. {
  385.     unsigned char *data;
  386.  
  387.     data = (unsigned char *) malloc(sizeof(char) * 5);
  388.     if(data == NULL)
  389.     {
  390.           fprintf(stderr,"Sorry, out of memory!\n");
  391.           exit(1);
  392.     }
  393.     data[0] = hour;
  394.     data[1] = min;
  395.     data[2] = sec;
  396.     data[3] = frame;
  397.     data[4] = fract;
  398.     add_packet((unsigned long)Mf_currtime,smpte_offset,META_EVENT,0,data,(unsigned long)5);
  399. }
  400.  
  401. myprogram(chan,program)
  402. int chan, program;
  403. {
  404.     unsigned char *data;
  405.     data = (unsigned char *) malloc(sizeof(char) * 1);
  406.     if(data == NULL)
  407.     {
  408.           fprintf(stderr,"Sorry, out of memory!\n");
  409.           exit(1);
  410.     }
  411.     data[0] = (unsigned char) program;
  412.     add_packet((unsigned long)Mf_currtime,program_chng,MIDI_EVENT,chan,data,(unsigned long)1);
  413. }
  414.  
  415. mypitchbend(chan,msb,lsb)
  416. int chan, msb,lsb;
  417. {
  418.     unsigned char *data;
  419.     data = (unsigned char *) malloc(sizeof(char) * 2);
  420.     if(data == NULL)
  421.     {
  422.           fprintf(stderr,"Sorry, out of memory!\n");
  423.           exit(1);
  424.     }
  425.     data[0] = (unsigned char) msb;
  426.     data[1] = (unsigned char) lsb;
  427.     add_packet((unsigned long)Mf_currtime,pitch_wheel,MIDI_EVENT,chan,data,(unsigned long)2);
  428. }
  429.  
  430. mysysex(len,msg)
  431. int len;
  432. char *msg;
  433. {
  434.     unsigned char *data;
  435.     int i;
  436.  
  437.     data = (unsigned char *) malloc((unsigned)(sizeof(char) * len));
  438.     if(data == NULL)
  439.     {
  440.           fprintf(stderr,"Sorry, out of memory!\n");
  441.           exit(1);
  442.     }
  443.     for(i = 0; i < len; i++)
  444.         data[i] = msg[i];
  445.     add_packet((unsigned long)Mf_currtime,system_exclusive,MIDI_EVENT,0,data,(unsigned long)len);
  446. }
  447.  
  448. myseqnum(num)
  449. int num;
  450. {
  451.     unsigned char *data;
  452.     data = (unsigned char *) malloc(sizeof(char) * 1);
  453.     if(data == NULL)
  454.     {
  455.           fprintf(stderr,"Sorry, out of memory!\n");
  456.           exit(1);
  457.     }
  458.     data[0] = (unsigned char) num;
  459.     add_packet((unsigned long)Mf_currtime,sequence_number,META_EVENT,0,data,(unsigned long)1);
  460. }
  461.  
  462. mytimesig(numer,denom,clocks,qnotes)
  463. char numer,denom,clocks,qnotes;
  464. {
  465.     unsigned char *data;
  466.  
  467.     data = (unsigned char *) malloc(sizeof(char) * 4);
  468.     if(data == NULL)
  469.     {
  470.           fprintf(stderr,"Sorry, out of memory!\n");
  471.           exit(1);
  472.     }
  473.     data[0] = numer;
  474.     data[1] = denom;
  475.     data[2] = clocks;
  476.     data[3] = qnotes;
  477.     add_packet((unsigned long)Mf_currtime,time_signature,META_EVENT,0,data,(unsigned long)4);
  478. }
  479.  
  480. mykeysig(sharpflat, minor)
  481. int sharpflat, minor;
  482. {
  483.     unsigned char *data;
  484.  
  485.     data = (unsigned char *) malloc(sizeof(char) * 2);
  486.     if(data == NULL)
  487.     {
  488.           fprintf(stderr,"Sorry, out of memory!\n");
  489.           exit(1);
  490.     }
  491.     data[0] = sharpflat;
  492.     data[1] = minor;
  493.     add_packet((unsigned long)Mf_currtime,key_signature,META_EVENT,0,data,(unsigned long)2);
  494. }
  495.  
  496. init_funcs()
  497. {
  498.     sequence_start = 0L;
  499.     sequence_end = 0L;
  500.  
  501.     /* needed for reading */
  502.     Mf_getc = mygetc;
  503.     Mf_error = myerror;
  504.     Mf_header = myheader;
  505.     Mf_noteon = mynoteon;
  506.     Mf_noteoff = mynoteoff;
  507.     Mf_pressure = mypressure;
  508.     Mf_parameter = myparameter;
  509.     Mf_pitchbend = mypitchbend;
  510.     Mf_program = myprogram;
  511.     Mf_chanpressure = mykeypressure;
  512.     Mf_sysex = mysysex;
  513.     Mf_metamisc = myvarlen;    
  514.     Mf_seqspecific = myvarlen;    
  515.     Mf_seqnum =  myseqnum;
  516.     Mf_text = myvarlen;
  517.     Mf_timesig = mytimesig;
  518.     Mf_smpte = mysmpte;
  519.     Mf_tempo = mytempo;
  520.     Mf_keysig = mykeysig;
  521.  
  522.    /* needed for writing */
  523.     Mf_putc = myputc;
  524.     Mf_writetrack = mywritetrack;
  525. }
  526.